﻿using LiteDb;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Net.Http;
using System.Net.Http.Headers;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Windows.Forms;
using DidaManager.Native.SerialPort;
using System.Diagnostics.CodeAnalysis;

namespace DidaManager.Native.Download
{
    public enum ConfigType
    {
        Config,   // PORTConfig, DEVConfig, VARConfig, MQTTConfig, NetworkConfig
        Hj212,    // NetworkHj212
        Lua,      // 预留
        Unknown   // 默认
    }

    public class DownloadManager
    {
        private readonly NetworkDownload _networkDownload;
        private readonly SerialDownload _serialDownload;
        private bool _isSerialDownload = false;

        public DownloadManager(SerialPortManager serialPortManager, [AllowNull] Control invokeControl = null)
        {
            if (invokeControl == null)
            {
                throw new ArgumentNullException(nameof(invokeControl), "invokeControl cannot be null.");
            }
            _networkDownload = new NetworkDownload();
            _serialDownload = new SerialDownload(serialPortManager, invokeControl);
        }

        // 下载有两种方式
        // 1、通过串口下载
        // 2、通过网络下载
        public object DownloadViaSerial(object param)
        {
            _isSerialDownload = true;
            return _serialDownload.DownloadViaSerial(param);
        }

        public object DownloadViaNetwork(string downloadUrl)
        {
            _isSerialDownload = false;
            return _networkDownload.DownloadViaNetwork(downloadUrl);
        }

        public object StopDownload(object param)
        {
            if(_isSerialDownload)
            {
                return _serialDownload.StopDownload();
            }
            else
            {
                return _networkDownload.StopDownload();
            }
        }
    }
}
